library(RPostgreSQL)
dr=dbDriver("PostgreSQL")

str_dbname="fimo"
str_host="192.168.3.4"
str_port=5432
str_user="rasdaman"
str_password="rasdaman"



#query="SELECT grdstation.id,avg(exp((550 - 500)*((log(grdaeraot_data.aod_675) - log(grdaeraot_data.aod_500))/(675-500)) + log(grdaeraot_data.aod_500))) FROM apom.grdaeraot_data,public.grdstation WHERE grdaeraot_data.stationid = grdstation.id AND grdaeraot_data.stationid IN (68,69,70,71,72,73,74) AND grdaeraot_data.aqstime BETWEEN '2013-01-02 04:00:00' and '2013-01-02 06:00:00' group by ( grdstation.id)"


con=dbConnect(dr,dbname="fimo",host="192.168.3.4",port=5432,user="rasdaman",password="rasdaman")
#rs=dbSendQuery(con,query)
#rs=dbSendQuery(con,"select * from grdstation")
#fetch(rs,n=-1)
rs=dbGetQuery(con,"select pm1,pm25,pm10,radiation,barometer,temp,windspd,winddir,rh from apom.grdpmhiscem_data")

#PM1

pm1.radiation=rs[,c(1,4)]
pm1.radiation=subset(pm1.radiation,pm1!=0&radiation!=0)
nrow(pm1.radiation)
a=cor(pm1.radiation$pm1,pm1.radiation$radiation)
a*a

pm1.barometer=rs[,c(1,5)]
pm1.barometer=subset(pm1.barometer,pm1!=0&barometer!=0)
nrow(pm1.barometer)
cor(pm1.barometer)*cor(pm1.barometer)

pm1.temp=rs[,c(1,6)]
pm1.temp=subset(pm1.temp,pm1!=0&temp!=0)
nrow(pm1.temp)
cor(pm1.temp)*cor(pm1.temp)

pm1.windspd=rs[,c(1,7)]
pm1.windspd=subset(pm1.windspd,pm1!=0&windspd!=0)
nrow(pm1.windspd)
cor(pm1.windspd)*cor(pm1.windspd)


pm1.winddir=rs[,c(1,8)]
pm1.winddir=subset(pm1.winddir,pm1!=0&winddir!=0)
nrow(pm1.winddir)
cor(pm1.winddir)*cor(pm1.winddir)

pm1.rh=rs[,c(1,9)]
pm1.rh=subset(pm1.rh,pm1!=0&rh!=0)
nrow(pm1.rh)
cor(pm1.rh)*cor(pm1.rh)

#PM25

pm25.radiation=rs[,c(2,4)]
pm25.radiation=subset(pm25.radiation,pm25!=0&radiation!=0)
nrow(pm25.radiation)
cor(pm25.radiation)*cor(pm25.radiation)

pm25.barometer=rs[,c(2,5)]
pm25.barometer=subset(pm25.barometer,pm25!=0&barometer!=0)
nrow(pm25.barometer)
cor(pm25.barometer)*cor(pm25.barometer)

pm25.temp=rs[,c(2,6)]
pm25.temp=subset(pm25.temp,pm25!=0&temp!=0)
nrow(pm25.temp)
cor(pm25.temp)*cor(pm25.temp)

pm25.windspd=rs[,c(2,7)]
pm25.windspd=subset(pm25.windspd,pm25!=0&windspd!=0)
nrow(pm25.windspd)
cor(pm25.windspd)*cor(pm25.windspd)


pm25.winddir=rs[,c(2,8)]
pm25.winddir=subset(pm25.winddir,pm25!=0&winddir!=0)
nrow(pm25.winddir)
cor(pm25.winddir)*cor(pm25.winddir)

pm25.rh=rs[,c(2,9)]
pm25.rh=subset(pm25.rh,pm25!=0&rh!=0)
nrow(pm25.rh)
cor(pm25.rh)*cor(pm25.rh)


#PM10

pm10.radiation=rs[,c(3,4)]
pm10.radiation=subset(pm10.radiation,pm10!=0&radiation!=0)
nrow(pm10.radiation)
cor(pm10.radiation)*cor(pm10.radiation)

pm10.barometer=rs[,c(3,5)]
pm10.barometer=subset(pm10.barometer,pm10!=0&barometer!=0)
nrow(pm10.barometer)
cor(pm10.barometer)*cor(pm10.barometer)

pm10.temp=rs[,c(3,6)]
pm10.temp=subset(pm10.temp,pm10!=0&temp!=0)
nrow(pm10.temp)
cor(pm10.temp)*cor(pm10.temp)

pm10.windspd=rs[,c(3,7)]
pm10.windspd=subset(pm10.windspd,pm10!=0&windspd!=0)
nrow(pm10.windspd)
cor(pm10.windspd)*cor(pm10.windspd)


pm10.winddir=rs[,c(3,8)]
pm10.winddir=subset(pm10.winddir,pm10!=0&winddir!=0)
nrow(pm10.winddir)
cor(pm10.winddir)*cor(pm10.winddir)

pm10.rh=rs[,c(3,9)]
pm10.rh=subset(pm10.rh,pm10!=0&rh!=0)
nrow(pm10.rh)
cor(pm10.rh)*cor(pm10.rh)